package com.lzhpo.core.utils;

import com.google.common.collect.Lists;
import org.omg.CORBA.INTERNAL;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName: ListTool
 * @Description: 集合Handler
 * @Author: adobe
 * @Email software8888@163.com
 * @Version: v1.0
 */
public class ListTool {
    /**
     * @param lists 容错的目标集合的集合
     * @param  conditionFrist-conditionLast
     * @return 结果集
     * @throws Exception  容错不能大于等于集合数量!
     */
    public static List<String> getIdentical(List<List<String>> lists,
                                            int conditionFrist,
                                            int conditionLast) throws Exception {
        List<String> res=new ArrayList<>();
        List<String> allList=new ArrayList<>();
        List<String> finalAllList = allList;
        lists.forEach(e-> finalAllList.addAll(e));
        allList=finalAllList.stream().distinct().collect(Collectors.toList());
        int listsLen=lists.size();
        if(conditionFrist>=listsLen || conditionLast>=listsLen){
            throw new Exception("容错不能大于等于集合数量!");
        }
        res =allList.stream().filter(e->(listsLen- conditionLast)<=getContainNum(lists,e) && getContainNum(lists,e)<=(listsLen- conditionFrist)).collect(Collectors.toList());
        return res;
    }

    public static long getContainNum(List<List<String>> lists,String target){
        long num=0;
        List<List<String>> res;
        num=lists.stream().filter(e->e.contains(target)).count();
        return num;
    }


    public static List<Integer> getMaxNumsListStr(List<String> list,int size){
        List<Integer> sortedInts=   list.stream().map(s-> Integer.valueOf(s)).sorted().collect(Collectors.toList());
        return  sortedInts.stream().limit(size).collect(Collectors.toList());
    }

    public static List<Integer> getMaxNumsListInt(List<Integer> listStr,int size){
        List<Integer> sortedInts=   listStr.stream().sorted().collect(Collectors.toList());
        return  sortedInts.stream().limit(size).collect(Collectors.toList());
    }

    public static List<Integer> getMaxNums(String [] arr,int size){
        List<String> list= Stream.of(arr).collect(Collectors.toList());
        return  getMaxNumsListStr(list,size);
    }

    public static List<Integer> getMinNums(List<String > listStr,int size){
        List<Integer> sortedInts=   listStr.stream().map(s-> Integer.valueOf(s))
                .sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        return  sortedInts.stream().limit(size).collect(Collectors.toList());
    }

    public static void main(String[] args) {
        System.out.println(getMaxNumsListInt(Lists.newArrayList(4,3,1,3,9,33,22,6),5));
    }


    public static List<Integer> getFivePreThreeTestNumbers(List<Integer> listFirst,
                                                     int skip,
                                                     int n) {
        List<Integer> listFirstSortd = listFirst.stream().sorted().collect(Collectors.toList());
        List<Integer> result = Lists.newArrayList();
        List<Integer> firstInts = listFirstSortd.stream().skip(skip).limit(n).collect(Collectors.toList());
        for (int i = 0; i < firstInts.size(); i++) {
            int value = firstInts.get(i);
            int index = listFirst.indexOf(value);
            result.add(index);
            listFirst.set(index, -1);
        }
        return result.stream().sorted().collect(Collectors.toList());
    }

    public static List<Integer> getMaxTestNumbers(List<Integer> listFirst,
                                                           int skip,
                                                           int n) {
        List<Integer> listFirstSortd = listFirst.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        List<Integer> result = Lists.newArrayList();
        List<Integer> firstInts = listFirstSortd.stream().skip(skip).limit(n).collect(Collectors.toList());
        for (int i = 0; i < firstInts.size(); i++) {
            int value = firstInts.get(i);
            int index = listFirst.indexOf(value);
            result.add(index);
            listFirst.set(index, -1);
        }
        return result.stream().sorted().collect(Collectors.toList());
    }


}